package com.fowo.api.service.handleWork;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.entity.StockIn;
import com.fowo.api.entity.StockInProduct;
import com.fowo.api.model.stock.in.StockInPushStockOtherAction;
import com.fowo.api.model.stock.out.StockOutVo;
import com.fowo.api.model.stock.out.product.StockOutProductItemVo;
import com.fowo.api.service.impl.StockInProductServiceImpl;
import com.fowo.api.service.impl.StockInServiceImpl;
import com.fowo.api.service.impl.StockOutProductServiceImpl;
import com.fowo.api.service.impl.StockOutServiceImpl;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class stockInPushStockOther implements StockInPushStockOtherAction {
    @Resource
    private StockOutServiceImpl stockOutService;
    @Resource
    private StockOutProductServiceImpl stockOutProductService;
    @Resource
    private StockInServiceImpl stockInService;
    @Resource
    private StockInProductServiceImpl stockInProductService;
    @Override
    public void run(Long id) throws Exception {
        StockIn stockIn = stockInService.getById(id);
        Assert.notNull(stockIn,"未查到入库单!");
        Assert.isTrue(stockIn.getStatus().equals("1"),"入库单未审核!");
        addSellOutStockOrder(stockIn);
    }

    @Transactional(rollbackFor = Exception.class)
    public void addSellOutStockOrder(StockIn stockIn) {
        List<StockInProduct> stockInProductList = stockInProductService.list(Wrappers.<StockInProduct>query()
                .lambda()
                .eq(StockInProduct::getParentStockInId, stockIn.getId()));
        StockOutVo model =new StockOutVo();
        List<StockOutProductItemVo> stockOutProducts=new ArrayList<>();
        model.setType("CK002");
        model.setOutTime(new Date());
        model.setNeedOrg(stockIn.getNeedOrg());
        model.setStatus("CS01");
        model.setDept(stockIn.getApplicationDep());
        model.setWarehouse(stockIn.getWarehouse());
        model.setProductType(stockIn.getProductType());
        model.setRemark(stockIn.getRemarks());
        model.setAddType(stockIn.getAddType());
        for(int i=0;i<stockInProductList.size();i++){
            StockInProduct stockInProduct = stockInProductList.get(i);
            StockOutProductItemVo stockOutProductItemVo=new StockOutProductItemVo();
            stockOutProductItemVo.setProductId(stockInProduct.getProductId());
            stockOutProductItemVo.setProductName(stockInProduct.getProductName());
            stockOutProductItemVo.setSku(stockInProduct.getSku());
//            stockOutProductItemVo.setProductGoodNum(stockInProduct.getQuantity());
            stockOutProductItemVo.setQuantity(stockInProduct.getQuantity());
            stockOutProductItemVo.setPrice(stockInProduct.getUnitPrice());
            stockOutProductItemVo.setSubtotal(stockInProduct.getSubtotal());
            stockOutProducts.add(stockOutProductItemVo);
        }
        model.setStockOutProducts(stockOutProducts);
        try {
            stockOutService.create(model);
        } catch (Exception e) {
            throw new RuntimeException("下推其他出库单失败");
        }
    }
}
